<template>
  <div class="regis">
    <el-form ref="regisForm" :model="regisForm" :rules="regisRules" class="regis-form">
      <h3 class="title">围群博客注册</h3>
      <el-form-item prop="phone">
        <el-input v-model="regisForm.phone" type="text" auto-complete="off" placeholder="账号"
          prefix-icon="el-icon-user-solid">
          <!-- <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" /> -->
        </el-input>
      </el-form-item>

      <el-form-item prop="nickName">
        <el-input v-model="regisForm.nickName" type="text" auto-complete="off" placeholder="昵称"
          prefix-icon="el-icon-magic-stick">
        </el-input>
      </el-form-item>

      <el-form-item prop="pass">
        <el-input v-model="regisForm.pass" placeholder="密码" prefix-icon="el-icon-unlock">

        </el-input>
      </el-form-item>
      <el-form-item prop="checkPass">
        <el-input v-model="regisForm.checkPass" placeholder="确认密码" prefix-icon="el-icon-key">
        </el-input>
      </el-form-item>


      <el-form-item style="width:100%;">
        <el-button :loading="loading" size="medium" type="primary" style="width:100%;"
          @click.native.prevent="handleregis">
          <span v-if="!loading">立即注册</span>
          <span v-else>立即注册中...</span>
        </el-button>
        <div style="float: right;" v-if="register">
          <router-link class="link-type" :to="'/login'">《返回登录</router-link>
        </div>
      </el-form-item>
    </el-form>
    <Vcode :show="isShow" @success="success" @close="close" />
  </div>
</template>
      
<script>
import Vcode from "vue-puzzle-vcode";
export default {
  name: "regis",
  data() {
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.regisForm.pass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      codeUrl: "",
      isShow: false, // 验证码模态框是否出现
      regisForm: {
        phone: "",
        pass: "",
        nickName: "",
        checkPass: '',
      },
      regisRules: {
        phone: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        pass: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 注册开关
      register: true,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
  },
  components: {
    Vcode
  },
  methods: {
    changeCheckCode(_img) {//验证码点击刷新
      _img.src = "checkCode?" + new Date().getTime();
    },

    // 用户通过了验证
    success(msg) {
      this.isShow = false; // 通过验证后，需要手动隐藏模态框
      if (this.regisForm.pass !== this.regisForm.checkPass) {
        this.$message({
          message: '两次密码不一致',
          type: 'error'
        })
        return;
      }
      this.$axios.post("api/testLoginController/register",  this.regisForm).then((res) => {
        console.log(res);
        if (res.data.success) {
          this.$message({
            message: '注册成功,请登录',
            type: 'success'
          })
          this.$router.push({
            name: 'login'
          })
        } else {
          this.$message({
            message: res.data.message,
            type: 'error'
          })
        }
        this.loading = false;
      });
    },
    // 用户点击遮罩层，应该关闭模态框
    close() {
      this.isShow = false;
      this.loading = false;
    },

    handleregis() {
      this.loading = true;
      this.isShow = true;
    }
  }
};
</script>
    
<style scoped>
.imgCode {
  width: 50%;
}

.regis {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 700px;
  background-image: url("../../assets/images/login-background.jpg");
  background-size: cover;
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.regis-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;

  .el-input {
    height: 38px;

    input {
      height: 38px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.regis-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.regis-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-regis-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.regis-code-img {
  height: 38px;
}
</style>
    
    
      
      